package com.mantru.iotpot.Util

import android.content.Context
import android.content.SharedPreferences

object SharePreUtil {
    private const val CONFIG = "APP_VALUE"
    private var sp: SharedPreferences? = null
    fun saveStringData(context: Context, key: String?, value: String?) {
        if (sp == null) {
            sp = context.getSharedPreferences(
                CONFIG,
                Context.MODE_PRIVATE
            )
        }
        sp?.edit()?.putString(key, value)?.commit()
    }

    fun getStringData(context: Context, key: String?): String? {
        if (sp == null) {
            sp = context.getSharedPreferences(
                CONFIG,
                Context.MODE_PRIVATE
            )
        }
        return sp?.getString(key, "")
    }

    fun clearData(context: Context) {
        if (sp == null) {
            sp = context.getSharedPreferences(
                CONFIG,
                Context.MODE_PRIVATE
            )
        }
        val editor: SharedPreferences.Editor? = sp?.edit()
        if (editor != null) {
            editor.clear()
            editor.commit()
        }
    }
}
